const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Weather = sequelize.define('Weather', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  city: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notNull: {
        msg: '请提供城市名称'
      }
    }
  },
  province: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notNull: {
        msg: '请提供省份名称'
      }
    }
  },
  date: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  },
  temperature: {
    type: DataTypes.FLOAT,
    allowNull: false,
    validate: {
      notNull: {
        msg: '请提供当前温度'
      }
    }
  },
  temperatureFeelsLike: {
    type: DataTypes.FLOAT,
    allowNull: true,
    comment: '体感温度'
  },
  temperatureHigh: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  temperatureLow: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  humidity: {
    type: DataTypes.FLOAT,
    allowNull: true,
    validate: {
      min: 0,
      max: 100
    }
  },
  windSpeed: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  windDirection: {
    type: DataTypes.STRING,
    allowNull: true
  },
  weatherCondition: {
    type: DataTypes.ENUM(
      '晴', '多云', '阴', '小雨', '中雨', '大雨', '暴雨', 
      '雷阵雨', '小雪', '中雪', '大雪', '雾', '霾'
    ),
    allowNull: true
  },
  aqi: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  airQualityLevel: {
    type: DataTypes.ENUM(
      '优', '良', '轻度污染', '中度污染', '重度污染', '严重污染'
    ),
    allowNull: true
  },
  pm25: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  pm10: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  updateTime: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'weather',
  timestamps: false,
  indexes: [
    {
      fields: ['city', 'province']
    },
    {
      fields: ['date']
    }
  ]
});

module.exports = Weather; 